Micron Document
baubs git

Node / mirrors / respira / files / src / platform / electron / ElectronFileService.ts

Displaying Raw • Download

src/platform/electron/ElectronFileService.ts fix/pattern-rendering-and-coordinate-bugs (abf7b9a6) Text, 1.79 KB

import type { IFileService } from '../interfaces/IFileService';

/**
* Electron implementation of file service using native dialogs via IPC
*/
export class ElectronFileService implements IFileService {
async openFileDialog(): Promise<File | null> {
if (!window.electronAPI) {
throw new Error('Electron API not available');
}

try {
const result = await window.electronAPI.invoke<{ filePath: string; fileName: string } | null>('dialog:openFile', {
filters: [
{ name: 'PES Files', extensions: ['pes'] },
{ name: 'All Files', extensions: ['*'] },
],
});

if (!result) {
return null;
}

// Read the file content
const buffer = await window.electronAPI.invoke<ArrayBuffer>('fs:readFile', result.filePath);
const blob = new Blob([buffer]);
return new File([blob], result.fileName, { type: 'application/octet-stream' });
} catch (err) {
console.error('[ElectronFileService] Failed to open file:', err);
return null;
}
}

async saveFileDialog(data: Uint8Array, defaultName: string): Promise<void> {
if (!window.electronAPI) {
throw new Error('Electron API not available');
}

try {
const filePath = await window.electronAPI.invoke<string | null>('dialog:saveFile', {
defaultPath: defaultName,
filters: [
{ name: 'PEN Files', extensions: ['pen'] },
{ name: 'All Files', extensions: ['*'] },
],
});

if (filePath) {
await window.electronAPI.invoke('fs:writeFile', filePath, Array.from(data));
}
} catch (err) {
console.error('[ElectronFileService] Failed to save file:', err);
throw err;
}
}

hasNativeDialogs(): boolean {
return true;
}
}

Served by rngit 1.3.3 - Generated in 0.03s